package fragmentson;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import activity.SonActivity;
import bean.MessageForSimple;
import fragmentson.backhandle.BackHandledFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;

/**
 * 修改密码
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class ChangePassword extends BackHandledFragment {

	private View rootView;
	private EditText et_oldpassword,et_passwordone,et_passwordtwo;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_changepassword, null);
		initView();
		return rootView;
	}

	private void initView() {
		// TODO Auto-generated method stub
		((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.change_password);
		rootView.findViewById(R.id.tv_next).setOnClickListener(listener);
		et_oldpassword = ((EditText) rootView.findViewById(R.id.et_oldpassword));
		et_passwordone = ((EditText) rootView.findViewById(R.id.et_passwordone));
		et_passwordtwo = ((EditText) rootView.findViewById(R.id.et_passwordtwo));
	}

	public static ChangePassword newInstance() {
		ChangePassword fragment = new ChangePassword();
		return fragment;
	}

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.tv_next:
					if(!et_passwordone.getText().toString().equals(et_passwordtwo.getText().toString())){
						Toast.makeText(getActivity(),getResources().getString(R.string.error_password),Toast.LENGTH_LONG).show();
						return ;
					}
					ApiUtil.getApiService().setPassWord(DemoApplication.getToken(),et_oldpassword.getText().toString(),et_passwordtwo.getText().toString()).enqueue(new Callback<MessageForSimple>() {
						@Override
						public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
							try {
								MessageForSimple entity = response.body();
								if(entity.getCode().equals("10008")){
									((SonActivity) getActivity()).backTask();
								}else{
									Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}

						@Override
						public void onFailure(Call<MessageForSimple> call, Throwable t) {
							Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
						}
					});
					break;
				default:
					break;
			}
		}
	};

	@Override
	public boolean onBackPressed() {
		return false;
	}
	
}
